<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登录系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f4f4f9 0%, #e4e4f3 100%);
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            position: relative;
            overflow: hidden;
        }

        /* 背景装饰元素 */
        .decoration {
            position: absolute;
            opacity: 0.2;
            border-radius: 50%;
        }

        .decoration-1 {
            width: 200px;
            height: 200px;
            background-color: rgba(135, 206, 250, 0.2);
            top: 10%;
            left: 5%;
        }

        .decoration-2 {
            width: 150px;
            height: 150px;
            background-color: rgba(100, 149, 237, 0.2);
            top: 30%;
            right: 10%;
        }

        .decoration-3 {
            width: 250px;
            height: 250px;
            background-color: rgba(70, 130, 180, 0.2);
            bottom: 15%;
            left: 20%;
        }

        .nav {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 50px;
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            position: fixed;
            top: 0;
            z-index: 10;
        }

        .blog-title {
            font-size: 24px;
            font-weight: bold;
            color: #3498db;
            margin-left: 15px;
        }

        .nav img {
            height: 40px;
        }

        .space {
            flex-grow: 1;
        }

        .nav-span {
            margin: 0 15px;
            text-decoration: none;
            color: #555;
            font-weight: 500;
            transition: color 0.3s;
        }

        .nav-span:hover {
            color: #3498db;
        }

        .container-login {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin-top: 70px;
        }

        .login-dialog {
            background-color: #ffffff;
            width: 400px;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        .login-dialog h3 {
            text-align: center;
            margin-bottom: 30px;
            color: #3498db;
            font-size: 28px;
            font-weight: bold;
        }

        .row {
            margin-bottom: 20px;
            position: relative;
        }

        .row span {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
        }

        .row input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        .row input:focus {
            border-color: #3498db;
        }

        .input-icon {
            position: absolute;
            top: 10px;
            right: 15px;
            color: #aaa;
            font-size: 18px;
        }

        #togglePassword {
            cursor: pointer;
        }

        #confirm {
            display: flex;
            align-items: center;
        }

        #confirm input {
            width: 60%;
            margin-right: 10px;
        }

        #verificationCodeImg {
            width: 100px;
            height: 40px;
            margin: 0;
        }

        .captcha-hint {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
            text-align: center;
        }

        #submit {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        #submit:hover {
            background-color: #2980b9;
        }

        .register-button {
            width: 100%;
            padding: 12px;
            background-color: #f8f9fa;
            color: #3498db;
            border: 1px solid #3498db;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            text-align: center;
        }

        .register-button:hover {
            background-color: #eef5fc;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .nav {
                padding: 15px 20px;
            }

            .login-dialog {
                width: 90%;
                padding: 30px 20px;
            }
        }

        /* 验证错误样式 */
        .error-input {
            border-color: #e74c3c;
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .error-row {
            position: relative;
        }

        .error-row .error-message {
            display: block;
        }
    </style>
</head>
<body>
<!-- 背景装饰元素 -->
<div class="decoration decoration-1"></div>
<div class="decoration decoration-2"></div>
<div class="decoration decoration-3"></div>

<div class="nav">
    <img src="pic/logo2.jpg" alt="">
    <span class="blog-title">我的博客系统</span>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
</div>

<div class="container-login">
    <div class="login-dialog">
        <h3>用户登录</h3>
        <div class="row">
            <span><i class="fas fa-user"></i> 用户名</span>
            <input type="text" name="username" id="username" placeholder="请输入用户名">
            <i class="fas fa-user input-icon"></i>
        </div>
        <div class="row">
            <span><i class="fas fa-lock"></i> 密码</span>
            <input type="password" name="password" id="password" placeholder="请输入密码">
            <i class="fas fa-eye input-icon" id="togglePassword"></i>
        </div>

        <div class="row">
            <span><i class="fas fa-shield-alt"></i> 验证码</span>
            <div id="confirm">
                <input type="text" name="inputCaptcha" id="inputCaptcha" placeholder="请输入验证码">
                <img id="verificationCodeImg" src="/Captcha/get" style="cursor: pointer;" title="看不清？换一张" />
            </div>
            <div class="captcha-hint">点击图片刷新验证码</div>
        </div>


        <div class="row">
            <button id="submit" onclick="login()">提交</button>
        </div>

        <div class="row">
            <a href="register.html">
                <button type="button" class="register-button">注册新账号</button>
            </a>
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script>
    $("#verificationCodeImg").click(function(){
        $(this).hide().attr('src', '/Captcha/get?dt=' + Date.now()).fadeIn();
    });

    function login() {
        // 获取输入值
        const captchaVal = $("#inputCaptcha").val().trim();
        const usernameVal = $("#username").val().trim();
        const passwordVal = $("#password").val();

        // 前端验证
        if (!usernameVal) {
            alert("用户名不能为空");
            return;
        }
        if (!passwordVal) {
            alert("密码不能为空");
            return;
        }
        if (!captchaVal) {
            alert("验证码不能为空");
            return;
        }

        // 验证码检查
        $.ajax({
            type: "POST",
            url: "/Captcha/check",
            dataType: "json", // 明确指定响应类型
            data: { inCode: captchaVal },
            success: function(captchaResponse) {
                // 重命名变量避免冲突
                if (captchaResponse.message === "输入为空") {
                    alert("验证码不能为空");
                    refreshCaptcha();
                    return;
                }

                if (captchaResponse.data === "验证码正确") {
                    // 验证通过后执行登录
                    $.ajax({
                        type: "POST",
                        url: "/user/login",
                        dataType: "json", // 明确指定响应类型
                        data: {
                            userName: usernameVal,
                            password: passwordVal
                        },
                        success: function(loginResponse) {
                            // 使用不同变量名
                            if (loginResponse.code === "SUCCESS" && loginResponse.data) {
                                localStorage.setItem("user_token", loginResponse.data);
                                location.assign("blog_list.html");
                            } else {
                                alert(loginResponse.msg || "登录失败");
                                refreshCaptcha();
                            }
                        },
                        error: function(xhr) {
                            console.error("登录请求失败", xhr);
                            alert("登录服务异常 " );
                            refreshCaptcha();
                        }
                    });
                } else {
                    alert(captchaResponse.message || "验证码错误");
                    refreshCaptcha();
                }
            },
            error: function(error) {
                console.error("验证码请求失败", error);
                alert("验证码服务异常");
                refreshCaptcha();
            }
        });
    }

    // 刷新验证码函数
    function refreshCaptcha() {
        $("#verificationCodeImg")
            .hide()
            .attr('src', '/Captcha/get?dt=' + Date.now())
            .fadeIn();
        $("#inputCaptcha").val('').focus();
    }
</script>

</body>
</html>